import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { uploadUrl, downloadUrl, previewUrl } from "/@/views/full/affiche/Affiche.api";
import { render } from '/@/utils/common/renderUtils';

export const columns: BasicColumn[] = [
  { title: '类型', dataIndex: 'type', align: 'center',  width: '25px',
    customRender: ({ text }) => { return render.renderDict(text, 'afficheType'); }
  },
  { title: '标题', dataIndex: 'title', align: 'left', width: 200, },
  { title: '发布日期', dataIndex: 'issueDate', align: 'center', format: 'date|YYYY-MM-DD',width: 35,  },
  { title: '附件', dataIndex: 'cnt', align: 'center', width: 15,  },
  { title: '上传人', dataIndex: 'createBy', align: 'center', width: 25, },
  { title: '上传日期', dataIndex: 'createTime', align: 'center', format: 'date|YYYY-MM-DD HH:mm',width: 45,  },
];

export const searchFormSchema: FormSchema[] = [
  { label: 'stat', field: 'stat', component: 'InputNumber' },
  { label: 'content', field: 'content', component: 'Input'  },
];

export const formSchema: FormSchema[] = [
  // TODO 主键隐藏字段，目前写死为ID
  { label: '', field: 'id', component: 'Input', show: false },
  { label: '', field: 'attrIds', component: 'Input', show: false },
  { label: '附件', field: 'attachments', 
    component: 'JUpload',
    colProps: {
      span: 24,
    },
    componentProps: ({ formModel }) => {
      return {
        placeholder: ' ',
        // color: "success", 
        // size: "small", 
        data: {afficheId: formModel.id},
        bizPath:"official",
        action: uploadUrl,
        // 预览相关配置
        showPreview: true,
        showPreviewNumber: true,
        emptyHidePreview: false,
        // 文件操作URL
        downloadUrl: downloadUrl,
        previewUrl: previewUrl,
        // 显示类型配置
        listType: 'picture-card', // 或者 'text'、'picture'
      
        //delUrl: cmsFilesDelete,
        //downloadUrl: '',
        //listType: 'text',
        itemRender: null,
        returnUrl: false,
        onChange: (value) => {
          if (value) {
            try {
              // 解析上传返回的JSON数据
              const fileData = JSON.parse(value);
              // 提取所有文件的ID并保存到attrIds
              console.log
              if (Array.isArray(fileData)) {
                const fileIds = fileData.map(file => file.id);
                formModel.attrIds = fileIds.join(',');
                formModel.title = fileData[0].fileName;
              }
            } catch (e) {
              console.error('解析上传文件数据失败:', e);
            }
          }
        },
        style: {
          width: '100%',
          overflow: 'hidden',
          textOverflow: 'ellipsis',
          whiteSpace: 'nowrap'
        }
      };
    },
    rules: [{ required: true, message: '请选择上传文件' }],
  },
  { label: '类型', field: 'type', component: 'JDictSelectTag', rules: [{ required: true }],
    componentProps: {
      dictCode: 'afficheType',
      getPopupContainer: () => document.body,
    },
  },
  { 
    label: '标题', 
    field: 'title', 
    component: 'Input', 
    componentProps: { autoComplete: 'off' },
    required: true, 
    rules: [{ required: true }], 
  },
  { 
    label: '日期', 
    field: 'issueDate', 
    component: 'DatePicker', 
    componentProps: { 
      autoComplete: 'off',
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD',
      showTime: false,
      placeholder: '发布日期',
      style: "padding: 4px;",
      getPopupContainer: () => document.body,
    },
    required: true, 
    rules: [{ required: true }], 
  },
];
